package com.project.shtp.service.impl;

import com.project.shtp.dao.UserDao;
import com.project.shtp.pojo.web.vo.MyGoodsInfo;
import com.project.shtp.pojo.web.vo.OrderInquiry;
import com.project.shtp.pojo.web.dto.User;
import com.project.shtp.pojo.web.vo.HistoryBrowse;
import com.project.shtp.pojo.web.dto.PersonalInfoDTO;
import com.project.shtp.pojo.web.vo.PersonalInfoVO;
import com.project.shtp.pojo.web.vo.UserAllMessage;
import com.project.shtp.pojo.web.vo.UserCollection;
import com.project.shtp.pojo.web.vo.UserOrderWithGoods;
import com.project.shtp.service.UserService;
import com.project.shtp.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashSet;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;


    private HashSet<String> set = new HashSet<>();

   @Autowired
    UserDao userDao;

    @Override
    public CommonResult<Map> verificationCode(String data, HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @Override
    public CommonResult queryUserAllRole(Integer accountId) {
        String s = userDao.queryAccountInfo();
        return new CommonResult(200,"返回成功",s);
    }

    @Override
    public List<OrderInquiry> queryMyOrderForm(Integer userId, Integer statusId) {
        System.out.println("2----------------------------");
       return userDao.queryMyOrderForm(userId,statusId);
    }

    @Override
    public List<MyGoodsInfo> searchMyGoods(Integer userId) {
        return userDao.searchMyGoods(userId);
    }
    @Override
    public List<User> queryFollower(Integer userId){
        return userDao.queryFollower(userId);
    }

    @Override
    public List<HistoryBrowse> queryUserBrowse(Integer userId) {
        List<HistoryBrowse> historyBrowses = userDao.queryUserBrowse(userId);
        for (HistoryBrowse e : historyBrowses){
            e.setGoodsImage64(Base64.getEncoder().encodeToString(e.getGoodsImage()));
            e.setGoodsImage(null);
        }
        return historyBrowses;
    }

    @Override
    public List<User> queryUserFans(Integer userId) {

        return userDao.queryUserFans(userId);
    }

    @Override
    public int changeOrder(Integer orderId, Integer statusId) {
        return userDao.changeOrder(orderId, statusId);
    }

    @Override
    public CommonResult updatePersonalInfo(MultipartFile file, PersonalInfoDTO personalInfoDTO) {
        PersonalInfoVO user = new PersonalInfoVO();
        user.setUserId(personalInfoDTO.getUseId());
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            user.setUserImage(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setUserName(personalInfoDTO.getUserName());
        user.setUserSex(personalInfoDTO.getUserSex());
        user.setUserPhone(personalInfoDTO.getUserPhone());
        user.setUserSchool(personalInfoDTO.getUserSchool());
        user.setUserStudentId(personalInfoDTO.getUserStudentId());
        user.setPassWord(personalInfoDTO.getPassWord());
        user.setUserQq(personalInfoDTO.getUserQq());
        user.setUserWechat(personalInfoDTO.getUserWechat());
        userDao.updatePersonalInfo(user);
        return new CommonResult(200, "修改成功");
    }
    @Override
    public List<UserAllMessage> queryUserAllMessage(Integer userId) {
        return userDao.queryUserAllMessage(userId);
    }

    @Override
    public List<UserCollection> queryUserCollection(Integer userId) {
        return userDao.queryUserCollection(userId);
    }

    @Override
    public List<UserOrderWithGoods> queryOrderWithGoods(Integer userId) {
        return userDao.queryOrderWithGoods(userId);
    }


   @Override
    public Integer cancelTheOrder(Integer orderId){
        return userDao.cancelTheOrder(orderId);
    }

}
